package cn.yuanqiao.web.controller.archive.detection;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import cn.yuanqiao.archive.appManager.service.ITenantSysConfigService;
import cn.yuanqiao.archive.detection.domain.DetailLogs;
import cn.yuanqiao.archive.detection.domain.Detection;
import cn.yuanqiao.archive.detection.domain.DetectionPackage;
import cn.yuanqiao.archive.detection.service.IDetailLogsService;
import cn.yuanqiao.archive.shuzihua.service.DangAnJieShouService;
import cn.yuanqiao.common.exception.ServiceException;
import cn.yuanqiao.common.utils.StringUtils;
import cn.yuanqiao.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;

import cn.yuanqiao.archive.detection.domain.DetectionObject;
import cn.yuanqiao.archive.detection.service.IDetectionObjectService;
import cn.yuanqiao.common.web.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.utils.poi.ExcelUtil;
import cn.yuanqiao.common.web.page.TableDataInfo;

/**
 * 四性配置内容Controller
 * 
 * @author xy
 * @date 2023-08-08
 */
@RestController
@RequestMapping("/archive/detectionObject")
public class DetectionObjectController extends BaseController
{
    @Autowired
    private IDetectionObjectService detectionObjectService;

    @Autowired
    private ITenantSysConfigService sysConfigService;
    @Autowired
    private IDetailLogsService detailLogsService;

    @Value("${SERVER_IP}")
    private String ip;
    @Value("${is_detection}")
    private String isDetection;

    @Autowired
    private DangAnJieShouService dangAnJieShouService;
    /**
     * 查询四性配置内容列表
     */
/*
    @PreAuthorize("@ss.hasPermi('archive:DetectionObject:list')")
*/
    @GetMapping("/list")
    public TableDataInfo list(DetectionObject detectionObject)
    {
        startPage();
        //获取页面

        List<DetectionObject> list = detectionObjectService.selectDetectionObjectList(detectionObject);


        return getDataTable(list);
    }

    /**
     * 导出四性配置内容列表
     */
/*
    @PreAuthorize("@ss.hasPermi('archive:DetectionObject:export')")
*/
    @Log(title = "四性配置内容", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DetectionObject detectionObject)
    {
        List<DetectionObject> list = detectionObjectService.selectDetectionObjectList(detectionObject);
        ExcelUtil<DetectionObject> util = new ExcelUtil<DetectionObject>(DetectionObject.class);
        util.exportExcel(response, list, "四性配置内容数据");
    }

    /**
     * 获取四性配置内容详细信息
     */
/*
    @PreAuthorize("@ss.hasPermi('archive:DetectionObject:query')")
*/
    @GetMapping(value = "/{pId}")
    public AjaxResult getInfo(@PathVariable("pId") Long pId)
    {
        return success(detectionObjectService.selectDetectionObjectByPid(pId));
    }

    /**
     * 新增四性配置内容
     */
/*
    @PreAuthorize("@ss.hasPermi('archive:DetectionObject:add')")
*/
    @Log(title = "四性配置内容", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody List<DetectionObject> detectionObjectList)
    {
        return toAjax(detectionObjectService.insertDetectionObjectList(detectionObjectList));
    }

    /**
     * 修改四性配置内容
     */
/*
    @PreAuthorize("@ss.hasPermi('archive:DetectionObject:edit')")
*/
    @Log(title = "四性配置内容", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DetectionObject detectionObject)
    {
        return toAjax(detectionObjectService.updateDetectionObject(detectionObject));
    }

    /**
     * 删除四性配置内容
     */
/*
    @PreAuthorize("@ss.hasPermi('archive:DetectionObject:remove')")
*/
    @Log(title = "四性配置内容", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(detectionObjectService.deleteDetectionObjectByIds(ids));
    }
    //判断文件是否有详情
    @GetMapping("/detail")
    public AjaxResult detail(Long pId){
        int count=detectionObjectService.getDetailCount(pId);
        return success(count);
    }

    //四性检测
    @PostMapping("/detection")
    public AjaxResult detection(@RequestBody List<DetectionPackage>packages){
        Map map=detectionObjectService.detection(packages);
        return success(map);
    }

//    //四性检测
//    @GetMapping("/fileDetection")
//    public AjaxResult fileDetection(@RequestBody List<DetectionPackage>packages){
//        Map map=detectionObjectService.fileDetection(packages);
//        return success(map);
//    }

    //四性检测
    @PostMapping("/daDetection")
    public AjaxResult daDetection(@RequestBody DetectionPackage packages){
        Map map=detectionObjectService.daDetection(packages);
        return success(map);
    }

    //获取用户Id以及当前IP
    @PostMapping("/userMessage")
    public AjaxResult userMessage(){
      Long userId= SecurityUtils.getUserId();

       Map map=new HashMap();
       map.put("userId",userId);
       map.put("ip",ip);
       return success(map);
    }

    //四性检测
    @Log(title = "四性检测", businessType = BusinessType.OTHER)
    @PostMapping("/daIdsDetection")
    public AjaxResult daIdsDetection(@RequestBody DetectionPackage packages){
        //四性检测
       if("1".equals(isDetection)) {
           Map map = detectionObjectService.daIdsDetection(packages);
           return success(map);
       }else{
              String []str=packages.getDaIds().split(",");
               Long[] arr = new Long[str.length];

           for (int i = 0; i < str.length; i++) {
               arr[i] = Long.parseLong(str[i]);
           }

           dangAnJieShouService.jieshou(packages.getCategoryId(),arr);
           Map map=new HashMap();
           map.put("daId",new ArrayList<>());
           return AjaxResult.success(map);
       }
    }

    //四性检测
    @Log(title = "获取四性详情", businessType = BusinessType.OTHER)
    @GetMapping("/getDetail")
    public TableDataInfo getSxId(Long daId){
        DetailLogs detailLogs=new DetailLogs();
        Long sxId=detectionObjectService.getSxId(daId);
        detailLogs.setpId(sxId);
        startPage();
        List<DetailLogs> list = detailLogsService.selectDetailLogsList(detailLogs);
        return getDataTable(list);
      //  return success(sxId);
    }

    @Log(title = "移交接收四性检测", businessType = BusinessType.OTHER)
    @PostMapping("/handoverDetection")
    public AjaxResult handoverDetection(@RequestBody DetectionPackage packages){
        try{
            Map map= detectionObjectService.handoverDetection(packages);
            return AjaxResult.success(map);

        }catch (Exception ex){
            ex.printStackTrace();
            return AjaxResult.error("检测失败");

        }
    }
}
